package com.baomidou.ant.service.impl;

import com.baomidou.ant.entity.CheckInfo;
import com.baomidou.ant.entity.ReportOrder;
import com.baomidou.ant.entity.SysUser;
import com.baomidou.ant.mapper.SysUserMapper;
import com.baomidou.ant.service.*;
import com.baomidou.ant.utils.MyStringUtil;
import com.baomidou.ant.vo.request.RequestChangeReCheckVo;
import com.baomidou.ant.vo.request.RequestRegisterVo;
import com.baomidou.ant.vo.request.RequestUserListVo;
import com.baomidou.ant.vo.response.ResponseListUserVo;
import com.baomidou.ant.vo.response.ResponseUserListVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2022-03-18
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private IReportOrderService reportOrderService;
    @Autowired
    private ICheckInfoService checkInfoService;
    @Autowired
    private IProcessInfoService processInfoService;


    @Override
    public SysUser queryByLoginName(String loginName) {
        QueryWrapper<SysUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("login_name",loginName);
        List<SysUser> userList=list(queryWrapper);
        if(userList==null||userList.size()==0) {
            return null;
        }
        return userList.get(0);
    }


    @Override
    public void addUser(RequestRegisterVo requestRegisterVo) {
        SysUser user=new SysUser();
        //将vo信息存入user中
        BeanUtils.copyProperties(requestRegisterVo,user);
        user.setPassword(requestRegisterVo.getPassword());
        //设置创建人
        SysUser createUser=getById(requestRegisterVo.getCreateUserId());
        user.setCreateBy(createUser.getUserName());
        //设置部门Id
        user.setDeptId(requestRegisterVo.getDeptId());
        user.setStatus(true);
        //保存
        save(user);
    }

    @Override
    public IPage<ResponseUserListVo> userList(RequestUserListVo requestUserListVo) {
        //分页对象
        IPage<SysUser> userIPage=new Page<>(requestUserListVo.getDisplayStart(),requestUserListVo.getDisplayLength());
        //查询条件
        QueryWrapper<SysUser> queryWrapper=new QueryWrapper<>();
        //用户名查询
        if(StringUtils.isNotBlank(requestUserListVo.getUserName())){
            queryWrapper.like("user_name",requestUserListVo.getUserName());
        }
        //登录工号查询
        if(StringUtils.isNotBlank(requestUserListVo.getLoginName())){
            queryWrapper.like("login_name",requestUserListVo.getLoginName());
        }
        //部门查询
        if(StringUtils.isNotBlank(requestUserListVo.getDeptName())){
            Integer deptId=deptService.queryByDeptName(requestUserListVo.getDeptName());
            queryWrapper.eq("dept_id",deptId);
        }
        //得到结果集
        IPage<SysUser> userPage=page(userIPage,queryWrapper);
        //返回处理结果
        return userPage.convert(user -> {
            ResponseUserListVo vo=new ResponseUserListVo();
            BeanUtils.copyProperties(user,vo);
            if(user.getWorkScope()==null|| Objects.equals(user.getWorkScope(), "0")){
                vo.setProcessName("无限制");
            }else {
                int[] ids = MyStringUtil.toInt(user.getWorkScope());
                String[] processNames = new String[ids.length];
                for (int i = 0; i < processNames.length; i++) {
                    processNames[i] = processInfoService.getById(ids[i]).getProcessName();
                }
                vo.setProcessName(MyStringUtil.strToString(processNames));
            }
            String deptName=deptService.getById(user.getDeptId()).getDept();
            vo.setDeptName(deptName);
            return vo;
        });
    }

    @Override
    public List<ResponseListUserVo> queryByDept(Integer deptId) {
        QueryWrapper<SysUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("dept_id",deptId);
        queryWrapper.eq("status",true);
        List<SysUser> userList=list(queryWrapper);
        List<ResponseListUserVo> list=new ArrayList<>();
        for(SysUser user:userList){
            ResponseListUserVo vo=new ResponseListUserVo();
            BeanUtils.copyProperties(user,vo);
            list.add(vo);
        }
        return list;
    }

    @Override
    public void changeReCheck(RequestChangeReCheckVo vo) {
        QueryWrapper<ReportOrder> reportOrderQueryWrapper=new QueryWrapper<>();
        QueryWrapper<CheckInfo> checkInfoQueryWrapper=new QueryWrapper<>();
        //工号,唯一性查询条件
        if(ObjectUtils.isNotEmpty(vo.getOldLoginName())){
            reportOrderQueryWrapper.eq("worker_no",vo.getOldLoginName());
            checkInfoQueryWrapper.eq("checker_no",vo.getOldLoginName());
        }
        //如果改了用户名
        if(ObjectUtils.isNotEmpty(vo.getOldUserName())){
            reportOrderQueryWrapper.eq("process_worker",vo.getOldUserName());
            checkInfoQueryWrapper.eq("checker",vo.getOldUserName());
        }
        //更新上报数据
        List<ReportOrder> reportOrderList=reportOrderService.list(reportOrderQueryWrapper);
        if(reportOrderList!=null&&reportOrderList.size()>0){
            for(ReportOrder reportOrder:reportOrderList){
                //更新并保存
                if(ObjectUtils.isNotEmpty(vo.getNewUserName())){
                    reportOrder.setProcessWorker(vo.getNewUserName());
                }
                if(ObjectUtils.isNotEmpty(vo.getNewLoginName())){
                    reportOrder.setWorkerNo(vo.getNewLoginName());
                }
                reportOrderService.updateById(reportOrder);
            }
        }
        //更新审核数据
        List<CheckInfo> checkInfoList=checkInfoService.list(checkInfoQueryWrapper);
        if(checkInfoList!=null&&checkInfoList.size()>0){
            for(CheckInfo checkInfo:checkInfoList){
                //更新并保存
                if(ObjectUtils.isNotEmpty(vo.getNewUserName())){
                    checkInfo.setChecker(vo.getNewUserName());
                }
                if(ObjectUtils.isNotEmpty(vo.getNewLoginName())){
                    checkInfo.setCheckerNo(vo.getNewLoginName());
                }
                checkInfoService.updateById(checkInfo);
            }
        }
    }

}
